/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Core.Model.Data.Generics;
using Engine;

namespace Core.Model.Actions.Elementary
{
    public abstract class ElementaryAction
    {
      protected static readonly log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
        #region Exchanged Attributes

        private GenericObjectRef targetRef;

        public GenericObjectRef TargetRef
        {
            get { return targetRef; }
            set { targetRef = value; }
        }
        private Root target;

        internal Root Target
        {
            get { return target; }
            set { target = value; }
        }

        #endregion

        #region Constructors

        public ElementaryAction ( GenericObjectRef targetRef )
        {
            this.targetRef = targetRef;
        }

        public ElementaryAction () { }

        #endregion

        #region Actions
        
        //final
        public void execute ()
        {
            //  Look up the object referenced by targetRef, and run the action
            Target = Engine.MHEGEngine.Instance.Find( targetRef.Value );

            //try
            //{
            if(target != null)
                Run();
            else
                log.Warn("Trying to excecute elementary action ("+ToString() +")  null target ("+target+"). Ignoring");
            //}
            //catch (ClassCastException cce)
            //{
            // log.warning("ElementaryAction " + getActionName() + " was targeted at " + targetRef.getClass().getName().substring(targetRef.getClass().getName().lastIndexOf('.') + 1) + " which was of incorrect class");
            //}
            //catch (NullPointerException npe)
            // {
            //  log.warning("ElementaryAction " + getActionName() + " was executed with an invalid target, or with absent parameters");
            //}
            //catch (Exception e)
            //{
            // log.warning("ElementaryAction " + getActionName() + " caught an unhandled exception - " + e.getMessage());
            //}
        }

        public String getActionName ()
        {
            String name =  GetType().ToString();
            int i;

            if ( ( i = name.LastIndexOf( '.' ) ) != -1 )
                name = name.Substring( ++i );

            return name;
        }

        public override string ToString ()
        {
            return getActionName() + " on " + targetRef.Value.ToString();
        }

        internal abstract void Run ();


        #endregion
    }
}
